package com.example.demo.common;

import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: cheng
 * @date: 2023/6/28 17:35
 * @description:
 */
public class StringTools {

    /**
     * 文章摘要
     * @param val
     * @param maxLength
     * @return
     */
    public static String subLength(String val,int maxLength){
        if(!StringUtils.hasLength(val) || maxLength <= 0){
            return val;
        }
        if (val.length() <= maxLength){
            return val;
        }
        return val.substring(0,maxLength);

    }

    public static String removeKeywords(String text, String[] keywords) {
        for (String keyword : keywords) {
            Pattern pattern = Pattern.compile("\\b" + Pattern.quote(keyword) + "\\b");
            Matcher matcher = pattern.matcher(text);
            text = matcher.replaceAll("");
        }
        return text;
    }
}
